﻿using HZYProject.Core.Repositories;
using HZYProject.Data;
using HZYProject.Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HZYProject.Service.StudentServices
{
    public class StudentService : IStudentService
    {
        private readonly IRepository<Student, Guid> _repository;
        private readonly HZYProjectDbContext _dbContext;
        private readonly IRepository<Class, Guid> _classRepository;
        private readonly IRepository<User, Guid> _userRepository;

        public StudentService(IRepository<Student, Guid> repository, HZYProjectDbContext dbContext,IRepository<Class, Guid> classRepository, IRepository<User, Guid> userRepository)
        {
            _repository = repository;
            _dbContext = dbContext;
            _classRepository = classRepository;
            _userRepository = userRepository;
        }

        public async Task<PagedResultDto<Student>> GetPaginatedResult(GetStudentInput input)
        {
            var query = _repository.GetAll();
            //搜索
            if (!string.IsNullOrEmpty(input.Name))
            {
                query = query.Where(x => x.RealName.Contains(input.Name));
            }
            if (!(input.CID == Guid.Empty))
            {
                query = query.Where(x => x.CollegesId == input.CID);
            }
            //统计查询数据的总条数，用于分页计算总页数
            var count = query.Count();
            query = query.Include("Colleges").Include("Classes");
            //根据需求进行排序，然后进行分页逻辑的计算
            query = query.OrderByDescending(x => x.RealName).Skip((input.CurrentPage - 1) * input.MaxResultCount).Take(input.MaxResultCount);
            //将查询结果转换为List集合，加载到内存中
            var models = await query.AsNoTracking().ToListAsync();
            var dtos = new PagedResultDto<Student>
            {
                TotalCount = count,
                CurrentPage = input.CurrentPage,
                MaxResultCount = input.MaxResultCount,
                Data = models,
                FilterText = input.FilterText,
            };
            return dtos;
        }

        public void AddStu(string un,UStudent stu)
        {
            var user = _userRepository.FirstOrDefault(x => x.UserName == un);
            var pwd = "~" + stu.IdNo.Substring(12, 6);
            Student s = new Student();
            s.StudentID = Guid.NewGuid();
            s.RealName = stu.RealName;
            s.PassWord = pwd;
            s.CollegesId = user.CollegesID;
            s.IdNo = stu.IdNo;
            s.Sex = stu.Sex;
            s.Mphone = stu.Mphone;
            s.Email = stu.Email;
            s.CreateTime = DateTime.Now;
            s.Status = stu.Status;
            s.Classes.Clear();
            foreach (var item in stu.ClassID)
            {
                s.Classes.Add(_classRepository.FirstOrDefault(x => x.ClassID == item));
            }
            _dbContext.Students.Add(s);
            _dbContext.SaveChanges();
        }

    }
}
